package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserMedal;
import com.ruoyi.system.service.IUserMedalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户勋章Controller
 *
 * @author ruoyi
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/system/usermedal")
public class UserMedalController extends BaseController
{
    @Autowired
    private IUserMedalService userMedalService;

    /**
     * 查询用户勋章列表
     */
    @PreAuthorize("@ss.hasPermi('system:usermedal:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserMedal userMedal)
    {
        startPage();
        List<UserMedal> list = userMedalService.selectUserMedalList(userMedal);
        return getDataTable(list);
    }

    /**
     * 导出用户勋章列表
     */
    @PreAuthorize("@ss.hasPermi('system:usermedal:export')")
    @Log(title = "用户勋章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserMedal userMedal)
    {
        List<UserMedal> list = userMedalService.selectUserMedalList(userMedal);
        ExcelUtil<UserMedal> util = new ExcelUtil<UserMedal>(UserMedal.class);
        util.exportExcel(response, list, "用户勋章数据");
    }

    /**
     * 获取用户勋章详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:usermedal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userMedalService.selectUserMedalById(id));
    }

    /**
     * 新增用户勋章
     */
    @PreAuthorize("@ss.hasPermi('system:usermedal:add')")
    @Log(title = "用户勋章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserMedal userMedal)
    {
        return toAjax(userMedalService.insertUserMedal(userMedal));
    }

    /**
     * 修改用户勋章
     */
    @PreAuthorize("@ss.hasPermi('system:usermedal:edit')")
    @Log(title = "用户勋章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserMedal userMedal)
    {
        return toAjax(userMedalService.updateUserMedal(userMedal));
    }

    /**
     * 删除用户勋章
     */
    @PreAuthorize("@ss.hasPermi('system:usermedal:remove')")
    @Log(title = "用户勋章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userMedalService.deleteUserMedalByIds(ids));
    }

    /**
     * 获取当前登录用户的所有勋章列表
     */
    @GetMapping("/myMedals")
    public TableDataInfo myMedals()
    {
        Long userId = SecurityUtils.getUserId();
        startPage();
        List<UserMedal> list = userMedalService.selectUserMedalsByUserId(userId);
        return getDataTable(list);
    }
}